/**
 * This file is part of Aion X Emu <aionxemu.com>
 *
 *  This is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This software is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser Public License
 *  along with this software.  If not, see <http://www.gnu.org/licenses/>.
 */

package system.handlers.quest.brusthonin;

import java.util.Collections;

import com.aionemu.gameserver.model.EmotionType;
import com.aionemu.gameserver.model.gameobjects.Item;
import com.aionemu.gameserver.model.gameobjects.Npc;
import com.aionemu.gameserver.model.gameobjects.player.Player;
import com.aionemu.gameserver.model.templates.quest.QuestItems;
import com.aionemu.gameserver.network.aion.serverpackets.SM_DIALOG_WINDOW;
import com.aionemu.gameserver.network.aion.serverpackets.SM_EMOTION;
import com.aionemu.gameserver.network.aion.serverpackets.SM_PLAY_MOVIE;
import com.aionemu.gameserver.network.aion.serverpackets.SM_USE_OBJECT;
import com.aionemu.gameserver.questEngine.QuestEngine;
import com.aionemu.gameserver.questEngine.handlers.QuestHandler;
import com.aionemu.gameserver.questEngine.model.QuestEnv;
import com.aionemu.gameserver.questEngine.model.QuestState;
import com.aionemu.gameserver.questEngine.model.QuestStatus;
import com.aionemu.gameserver.utils.PacketSendUtility;
import com.aionemu.gameserver.utils.ThreadPoolManager;

/**
 *
 *
 */
public class _2093TheSecretPassage extends QuestHandler
{

	private final static int	questId	= 2093;
	private final static int[]	npc_ids	=
										{ 205150, 205159, 205164, 205197, 205198, 730174, 700395 };

	public _2093TheSecretPassage()
	{
		super(questId);
	}

	public static void main(String[] args)
	{
		QuestEngine.getInstance().addQuestHandler(new _2093TheSecretPassage());
	}

	@Override
	public void register()
	{
		qe.addQuestLvlUp(questId);
		for (int npc_id : npc_ids)
			qe.setNpcQuestData(npc_id).addOnTalkEvent(questId);
	}

	@Override
	public boolean onLvlUpEvent(QuestEnv env)
	{
		return defaultQuestOnLvlUpEvent(env, 2091);
	}

	@Override
	public boolean onDialogEvent(final QuestEnv env)
	{
		final Player player = env.getPlayer();
		final QuestState qs = player.getQuestStateList().getQuestState(questId);
		if (qs == null)
			return false;

		int var = qs.getQuestVarById(0);
		int targetId = 0;
		if (env.getVisibleObject() instanceof Npc)
			targetId = ((Npc) env.getVisibleObject()).getNpcId();

		if (qs.getStatus() == QuestStatus.REWARD)
		{
			if (targetId == 205150)
			{
				if (env.getDialogId() == -1)
					return sendQuestDialog(env, 10002);
				else
				{
					Item.removeItemFromInventoryByItemId(player, 182209012);
					return defaultQuestEndDialog(env);
				}
			}
			return false;
		}
		else if (qs.getStatus() != QuestStatus.START)
		{
			return false;
		}
		if (targetId == 205150)
		{
			switch (env.getDialogId())
			{
				case 25:
					if (var == 0)
						return sendQuestDialog(env, 1011);
				case 1012:
					PacketSendUtility.sendPacket(player, new SM_PLAY_MOVIE(0, 397));
					break;
				case 10000:
					if (var == 0)
					{
						qs.setQuestVarById(0, var + 1);
						updateQuestStatus(player, qs);
						PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(env.getVisibleObject().getObjectId(), 10));
						return true;
					}
			}
		}
		else if (targetId == 205159)
		{
			switch (env.getDialogId())
			{
				case 25:
					if (var == 1)
						return sendQuestDialog(env, 1352);
				case 10001:
					if (var == 1)
					{
						qs.setQuestVarById(0, var + 1);
						updateQuestStatus(player, qs);
						PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(env.getVisibleObject().getObjectId(), 10));
						return true;
					}
			}
		}
		else if (targetId == 205164)
		{
			switch (env.getDialogId())
			{
				case 25:
					if (var == 2)
						return sendQuestDialog(env, 1693);
				case 10002:
					if (var == 2)
					{
						qs.setQuestVarById(0, var + 1);
						updateQuestStatus(player, qs);
						PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(env.getVisibleObject().getObjectId(), 10));
						return true;
					}
			}
		}
		else if (targetId == 205197)
		{
			switch (env.getDialogId())
			{
				case 25:
					if (var == 3)
						return sendQuestDialog(env, 2034);
					if (var == 4)
						return sendQuestDialog(env, 2375);
				case 10003:
					if (var == 3)
					{
						qs.setQuestVarById(0, var + 1);
						updateQuestStatus(player, qs);
						PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(env.getVisibleObject().getObjectId(), 10));
						return true;
					}
				case 10004:
					if (var == 4)
					{
						PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(env.getVisibleObject().getObjectId(), 10));
						return true;
					}
				case 33:
					if (var == 4)
					{
						if (QuestEngine.collectItemCheck(env, true))
						{
							qs.setQuestVarById(0, var + 1);
							updateQuestStatus(player, qs);
							return sendQuestDialog(env, 10000);
						}
						else
							return sendQuestDialog(env, 10001);
					}
			}
		}
		else if (targetId == 205198)
		{
			switch (env.getDialogId())
			{
				case 25:
					if (var == 5)
						return sendQuestDialog(env, 2716);
					else if (var == 7)
						return sendQuestDialog(env, 3398);
				case 3399:
					PacketSendUtility.sendPacket(player, new SM_PLAY_MOVIE(0, 398));
					Item.removeItemFromInventoryByItemId(player, 182209011);
					break;
				case 10005:
					if (var == 5)
					{
						qs.setQuestVarById(0, var + 1);
						updateQuestStatus(player, qs);
						PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(env.getVisibleObject().getObjectId(), 10));
						return true;
					}
				case 10007:
					if (var == 7)
					{
						qs.setQuestVarById(0, var + 1);
						updateQuestStatus(player, qs);
						PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(env.getVisibleObject().getObjectId(), 10));
						return true;
					}
			}
		}
		else if (targetId == 730174)
		{
			switch (env.getDialogId())
			{
				case -1:
					if (var == 6)
					{
						final int targetObjectId = env.getVisibleObject().getObjectId();
						PacketSendUtility.sendPacket(player, new SM_USE_OBJECT(player.getObjectId(), targetObjectId, 3000, 1));
						PacketSendUtility.broadcastPacket(player, new SM_EMOTION(player, EmotionType.NEUTRALMODE2, 0, targetObjectId), true);
						ThreadPoolManager.getInstance().schedule(new Runnable()
						{
							@Override
							public void run()
							{
								Npc npc = (Npc) player.getTarget();
								if (npc == null || npc.getObjectId() != targetObjectId)
									return;
								PacketSendUtility.sendPacket(player, new SM_USE_OBJECT(player.getObjectId(), targetObjectId, 3000, 0));
								PacketSendUtility.broadcastPacket(player, new SM_EMOTION(player, EmotionType.START_LOOT, 0, targetObjectId), true);

								Item.addItems(player, Collections.singletonList(new QuestItems(182209011, 1)));
								qs.setQuestVarById(0, qs.getQuestVarById(0) + 1);
								updateQuestStatus(player, qs);
							}

						}, 3000);
						return false;
					}
			}
		}
		else if (targetId == 700395)
		{
			switch (env.getDialogId())
			{
				case -1:
					if (var == 8)
					{
						final int targetObjectId = env.getVisibleObject().getObjectId();
						PacketSendUtility.sendPacket(player, new SM_USE_OBJECT(player.getObjectId(), targetObjectId, 3000, 1));
						PacketSendUtility.broadcastPacket(player, new SM_EMOTION(player, EmotionType.NEUTRALMODE2, 0, targetObjectId), true);
						ThreadPoolManager.getInstance().schedule(new Runnable()
						{
							@Override
							public void run()
							{
								Npc npc = (Npc) player.getTarget();
								if (npc == null || npc.getObjectId() != targetObjectId)
									return;
								PacketSendUtility.sendPacket(player, new SM_USE_OBJECT(player.getObjectId(), targetObjectId, 3000, 0));
								PacketSendUtility.broadcastPacket(player, new SM_EMOTION(player, EmotionType.START_LOOT, 0, targetObjectId), true);

								Item.addItems(player, Collections.singletonList(new QuestItems(182209012, 1)));
								qs.setStatus(QuestStatus.REWARD);
								updateQuestStatus(player, qs);
							}

						}, 3000);
						return false;
					}
			}
		}
		return false;
	}
}
